﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using NLBS.Entity;
using NLBS.Framework;

namespace NLBS.Data.Business
{
    /// <summary>
    /// 用于用户群组缓存操作
    /// </summary>
   public class UserGroupData:CommonBase
    {
       private static readonly string UserGroupKey = "UserGroupKey";

       public static List<UserGroup> GetUserGroup()
        {
            ContentCache cache = ContentCache.Instantiate();
            List<UserGroup> lists = (List<UserGroup>)cache[UserGroupKey];
            if (lists == null)
            {
                lists = new List<UserGroup>(DbProvider.Instance().GetUserGroup());
                if (lists != null)
                {
                    cache.Insert(UserGroupKey, lists, CacheDuration.Long);
                }
            }
            return lists;
        }

       public static List<UserGroup> UserGroupList
       {
           get
           {
               return GetUserGroup();
           }
       }

       public static UserGroup GetUserGroupByID(int id)
       {
           return UserGroupList.Find(delegate(UserGroup u)
           {
               return u.GroupID == id;
           });

       }
        public static void InsertUserGroup(UserGroup group)
        {
            int result = DbProvider.Instance().InsertUserGroup(group);
            group.GroupID = result;
            UserGroupList.Add(group);
        }

        public static void UpdateUserGroup(UserGroup group)
        {
            DbProvider.Instance().UpdateUserGroup(group);
            //UserGroup updategroup = GetUserGroupByID(group.GroupID);
            //IList<UserGroup> list = GetUserGroup();
            //list[list.IndexOf(updategroup)] = group;

        }

        public static void DeleteUserGroup(int groupID)
        {
            DbProvider.Instance().DeleteUserGroup(groupID);
            UserGroup group = GetUserGroupByID(groupID);
            UserGroupList.Remove(group);
        }
    }
}
